package org.chen.jenkins.client.api;

import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.QueueReference;
import org.chen.jenkins.client.dto.BuildDTO;
import org.chen.jenkins.client.entity.JenkinsJobInfo;
import org.chen.jenkins.client.entity.TfsProject;
import org.chen.jenkins.client.service.JenkinsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * @author chensj
 * @date 2019-09-19 16:23
 */
@RestController
public class JenkinsController {
    @Autowired
    private JenkinsService jenkinsService;

    /**
     * 获取Jenkins上面全部是的任务信息
     *
     * @return map
     */
    @GetMapping(value = "/jenkins")
    public Map<String, Job> getJenkins() throws IOException, URISyntaxException, InterruptedException {
        return jenkinsService.getJobs();
    }

    /**
     * 通过接口在Jenkins上面创建一个Maven任务
     *
     * @return str
     */
    @GetMapping(value = "/jenkins/create")
    public String create() throws IOException, URISyntaxException {
        jenkinsService.createJob();
        return "Jenkins Job 创建成功";
    }

    /**
     * 通过接口删除上面创建的jenkins任务
     *
     * @return str
     */
    @GetMapping(value = "/jenkins/delete")
    public String delete() throws IOException, URISyntaxException {
        jenkinsService.deleteJob();
        return "Jenkins Job 删除成功";
    }

    /**
     * 获取job详情
     *
     * @param jobName
     * @return
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @GetMapping(value = "/jenkins/list")
    public String getJenkinsList(@RequestParam String jobName) throws IOException, URISyntaxException,
            InterruptedException {
        return jenkinsService.getDetailList(jobName);
    }

    /**
     * 获取job的jobXML信息
     *
     * @param jobName 选定的任务
     * @return
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @GetMapping(value = "/jenkins/xml")
    public String getJenkinsXml(@RequestParam String jobName) throws IOException, URISyntaxException,
            InterruptedException {
        return jenkinsService.getJobXml(jobName);
    }

    /**
     * 复制任务
     * 将8080上面的任务复制到9080上面
     *
     * @param jobName 选定的任务
     * @return
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @GetMapping(value = "/jenkins/create/xml")
    public String copyJob(@RequestParam String jobName) throws IOException, URISyntaxException,
            InterruptedException {
        return jenkinsService.createJobByJobXml(jobName);
    }

    /**
     * 获取JobName的任务，并以此作为模板，
     * 请求TFS获取全部的模块信息，两者组合生成Jenkins job数据
     *
     * @param jobName 选定的任务
     * @return list
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @GetMapping(value = "/jenkins/create/all")
    public List<JenkinsJobInfo> copyForAll(@RequestParam String jobName) throws IOException, URISyntaxException,
            InterruptedException {
        return jenkinsService.createJobForAll(jobName);
    }

    //@GetMapping(value = "/jenkins/delete/all")
    //public String deleteAllJob() throws IOException, URISyntaxException,
    //        InterruptedException {
    //    return jenkinsService.deleteJobForAll();
    //}

    /**
     * 根据调用接口后返回结果删除Jenkins job数据
     *
     * @return
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @GetMapping(value = "/jenkins/delete/result")
    public String deleteAllJobByResult() throws IOException, URISyntaxException,
            InterruptedException {
        return jenkinsService.deleteAllJobByResult();
    }

    /**
     * 触发jenkins job
     *
     * @param jobName
     * @return QueueReference
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @GetMapping(value = "/jenkins/build")
    public QueueReference build(@RequestParam String jobName) throws IOException, URISyntaxException,
            InterruptedException {
        return jenkinsService.build(jobName);
    }

    /**
     * 获取指定buildNum的构建信息
     *
     * @param jobName
     * @param buildNum
     * @return str
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @GetMapping(value = "/jenkins/build/item")
    public String build(@RequestParam String jobName, @RequestParam Integer buildNum) throws IOException,
            URISyntaxException, InterruptedException {
        return jenkinsService.buildItemInfo(jobName, buildNum);
    }

    /**
     * 获取Job当前所处状态
     *
     * @param jobName
     * @return dto
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @GetMapping(value = "/jenkins/check")
    public BuildDTO check(@RequestParam String jobName) throws IOException,
            URISyntaxException, InterruptedException {
        return jenkinsService.checkIsBuild(jobName);
    }

}
